package com.yang.order.service;

import com.yang.order.dao.OrderDao;
import com.yang.springcloud.pojo.Order;
import com.yang.springcloud.service.AccountClientService;
import com.yang.springcloud.service.GoodClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImp implements OrederService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private AccountClientService accountClientService;

    @Autowired
    private GoodClientService goodClientService;

    @Override
    @Transactional
    public Map<String, Object> addOrder(Order o) {
        Map<String, Object> amsg = accountClientService.updateBalance(o.getGoodListStruct(), o.getOrderId());
        if (Integer.parseInt(String.valueOf(amsg.get("code"))) == 200) {
            Map<String, Object> gmsg = goodClientService.updateStock(o.getGoodListStruct());
            if (Integer.parseInt(String.valueOf(amsg.get("code"))) == 200){
                return orderDao.addOrder(o);
            }

            Map<String, Object> msg = new HashMap<String, Object>();
            msg.put("massage", "下单失败");
            msg.put("code", 400);
            return msg;
        }

        Map<String, Object> msg = new HashMap<String, Object>();
        msg.put("massage", "下单失败");
        msg.put("code", 400);
        return msg;
    }
}
